// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.weather.fx;

import zombie.debug.LineDrawer;
import zombie.iso.IsoCamera;

public class ParticleRectangle {
    protected boolean DEBUG_BOUNDS = false;
    private int width;
    private int height;
    private WeatherParticle[] particles;
    private int particlesToRender;
    private int particlesReqUpdCnt = 0;

    public ParticleRectangle(int w, int h) {
        this.width = w;
        this.height = h;
    }

    public int getWidth() {
        return this.width;
    }

    public int getHeight() {
        return this.height;
    }

    public void SetParticles(WeatherParticle[] weatherParticle) {
        for (int _int = 0; _int < weatherParticle.length; _int++) {
            weatherParticle[_int].setParent(this);
        }

        this.particles = weatherParticle;
        this.particlesToRender = weatherParticle.length;
    }

    public void SetParticlesStrength(float str) {
        this.particlesToRender = (int)((float)this.particles.length * str);
    }

    public boolean requiresUpdate() {
        return this.particlesToRender > 0 || this.particlesReqUpdCnt > 0;
    }

    public void update(float delta) {
        this.particlesReqUpdCnt = 0;

        for (int _int = 0; _int < this.particles.length; _int++) {
            WeatherParticle weatherParticle = this.particles[_int];
            if (_int < this.particlesToRender) {
                weatherParticle.alphaFadeMod.setTarget(1.0F);
            } else if (_int >= this.particlesToRender) {
                weatherParticle.alphaFadeMod.setTarget(0.0F);
            }

            weatherParticle.update(delta);
            if (weatherParticle.renderAlpha > 0.0F) {
                this.particlesReqUpdCnt++;
            }
        }
    }

    public void render() {
        int int0 = IsoCamera.frameState.playerIndex;
        int int1 = IsoCamera.frameState.OffscreenWidth;
        int int2 = IsoCamera.frameState.OffscreenHeight;
        int int3 = (int)Math.ceil((double)(int1 / this.width)) + 2;
        int int4 = (int)Math.ceil((double)(int2 / this.height)) + 2;
        int int5;
        if (IsoCamera.frameState.OffX >= 0.0F) {
            int5 = (int)IsoCamera.frameState.OffX % this.width;
        } else {
            int5 = this.width - (int)Math.abs(IsoCamera.frameState.OffX) % this.width;
        }

        int int6;
        if (IsoCamera.frameState.OffY >= 0.0F) {
            int6 = (int)IsoCamera.frameState.OffY % this.height;
        } else {
            int6 = this.height - (int)Math.abs(IsoCamera.frameState.OffY) % this.height;
        }

        int int7 = -int5;
        int int8 = -int6;

        for (int int9 = -1; int9 < int4; int9++) {
            for (int int10 = -1; int10 < int3; int10++) {
                int int11 = int7 + int10 * this.width;
                int int12 = int8 + int9 * this.height;
                if (this.DEBUG_BOUNDS || IsoWeatherFX.DEBUG_BOUNDS) {
                    LineDrawer.drawRect((float)int11, (float)int12, (float)this.width, (float)this.height, 0.0F, 1.0F, 0.0F, 1.0F, 1);
                }

                for (int int13 = 0; int13 < this.particles.length; int13++) {
                    WeatherParticle weatherParticle = this.particles[int13];
                    if (!(weatherParticle.renderAlpha <= 0.0F) && weatherParticle.isOnScreen((float)int11, (float)int12)) {
                        weatherParticle.render((float)int11, (float)int12);
                        if (this.DEBUG_BOUNDS || IsoWeatherFX.DEBUG_BOUNDS) {
                            LineDrawer.drawRect(
                                (float)(int11 + weatherParticle.bounds.getX()),
                                (float)(int12 + weatherParticle.bounds.getY()),
                                (float)weatherParticle.bounds.getWidth(),
                                (float)weatherParticle.bounds.getHeight(),
                                0.0F,
                                0.0F,
                                1.0F,
                                0.5F,
                                1
                            );
                        }
                    }
                }
            }
        }
    }
}
